package com.android.delivery.util.print;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.lvrenyang.io.Pos;

import static com.wisdom.tian.cpp.NativeUtil.convBitmap2Command;
import static com.wisdom.tian.cpp.NativeUtil.getPrintPictureCommand;
import static com.wisdom.tian.cpp.NativeUtil.getPrintPictureLineBufferCommand;
import static com.wisdom.tian.cpp.NativeUtil.getPrintPictureLineCommand;

/**
 * Created by jc on 2017/11/10.
 */
public class ImageUtil {

    public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
        // load the origial Bitmap
        Bitmap BitmapOrg = bitmap;

        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = w;
        int newHeight = h;

        // calculate the scale
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the Bitmap
        matrix.postScale(scaleWidth, scaleHeight);
        // if you want to rotate the Bitmap
        // matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);

        // make a Drawable from Bitmap to allow to set the Bitmap
        // to the ImageView, ImageButton or what ever
        return resizedBitmap;
    }

    /**将view转化为bitmap并且保存本地**/
    public static Bitmap getViewBitmap(View view) {
        view.clearFocus();
        view.setPressed(false);

        boolean willNotCache = view.willNotCacheDrawing();
        view.setWillNotCacheDrawing(false);

        int color = view.getDrawingCacheBackgroundColor();
        view.setDrawingCacheBackgroundColor(0);

        if (color != 0) {
            view.destroyDrawingCache();
        }
        view.buildDrawingCache();
        Bitmap cacheBitmap = view.getDrawingCache();
        if (cacheBitmap == null) {
            return null;
        }

        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

        view.destroyDrawingCache();
        view.setWillNotCacheDrawing(willNotCache);
        view.setDrawingCacheBackgroundColor(color);

        return bitmap;
    }

    /**
     * 按照一定的宽高比例裁剪图片
     * @param bitmap 要裁剪的图片
     * @param num1 长边的比例
     * @param num2 短边的比例
     * @param isRecycled 是否回收原图片
     * @return 裁剪后的图片
     */
    public static Bitmap imageCropAll(Bitmap bitmap, int num1, int num2, boolean isRecycled){
        if (bitmap == null){
            return null;
        }
        int w = bitmap.getWidth(); // 得到图片的宽，高
        int h = bitmap.getHeight();
        int retX, retY;
        int nw, nh;
        if (w > h){
            if (h > w * num2 / num1){
                nw = w;
                nh = w * num2 / num1;
                retX = 0;
                retY = (h - nh) / 2;
            } else{
                nw = h * num1 / num2;
                nh = h;
                retX = (w - nw) / 2;
                retY = 0;
            }
        } else{
            if (w > h * num2 / num1){
                nh = h;
                nw = h * num2 / num1;
                retY = 0;
                retX = (w - nw) / 2;
            } else{
                nh = w * num1 / num2;
                nw = w;
                retY = (h - nh) / 2;
                retX = 0;
            }
        }
        LogUtils.e("imageCrop","-----------imageCrop--retX->"+retX+";retY="+retY+";nw="+nw+";nh="+nh);
        Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,true);
        if (isRecycled && bitmap != null && !bitmap.equals(bmp)&& !bitmap.isRecycled()){
            bitmap.recycle();//回收原图片
            bitmap = null;
        }
        return bmp;
    }

    // print
    // 使用命令
    // HS 'L' n w_bytes repeat start dat[n]
    // n数据字节数。
    // repeat是否重复
    // start为偏移字节
    private static int dotPixel2PosCmd(byte[] dotsmap, byte []dstbuf, int nWidth, int nHeight)
    {
        return getPrintPictureLineCommand(dstbuf, dotsmap, nWidth, nHeight);
    }

    // print
    // 使用命令
    // HS 'l' n w_bytes repeat start dat[n]
    // n数据字节数。
    // repeat是否重复
    // start为偏移字节
    private static int dotPixel2PosMdjbtCmd(byte[] dotsmap, byte []dstbuf, int nWidth, int nHeight)
    {
        return getPrintPictureLineBufferCommand(dstbuf, dotsmap, nWidth, nHeight);
    }
	
	public static byte[] bitmap2PrintDotsmap(Bitmap mBitmap, int nWidth, boolean bPicture){
        int dstw = (nWidth + 7) / 8 * 8;
        int dsth = mBitmap.getHeight() * dstw / mBitmap.getWidth();
        boolean[] dithered = Pos.getBitmapDots(mBitmap, nWidth, bPicture);
        int w_bytes = (dstw + 7) /8;
        byte []dotsmap = new byte[w_bytes * dsth];
        for (int i=0; i<dsth; i++){
            for (int j=0; j<dstw; j++){
                if (dithered[i*dstw + j]){
                    dotsmap[i*w_bytes + j/8] |= 0x80 >> (j&7);
                }
            }
        }
        return dotsmap;
    }

	public static byte[] Bitmap2PosCmd(Bitmap mBitmap, int nWidth, boolean bPicture){
        int dstw = (nWidth + 7) / 8 * 8;
        int dsth = mBitmap.getHeight() * dstw / mBitmap.getWidth();

        byte []dotsmap = bitmap2PrintDotsmap(mBitmap, nWidth, bPicture);
        int size = dotPixel2PosCmd(dotsmap, null, dstw, dsth);
        if (size > 0){
            byte []buffer = new byte[size];
            size = dotPixel2PosCmd(dotsmap, buffer, dstw, dsth);
            return buffer;
        }
        return null;
    }

    public static byte[] Bitmap2PosCmd(Bitmap mBitmap, Bitmap pic_bitmap, int nWidth, boolean bPic){
        int dstw = (nWidth + 7) / 8 * 8;
        int dsth = mBitmap.getHeight() * dstw / mBitmap.getWidth();

        byte []dotsmap = bitmap2PrintDotsmap(mBitmap, nWidth, bPic);

        int size = dotPixel2PosCmd(dotsmap, null, dstw, dsth);
        if (size > 0){
            byte []buffer = new byte[size];
            size = dotPixel2PosCmd(dotsmap, buffer, dstw, dsth);
            return buffer;
        }
        return null;
    }

    public static byte[] Bitmap2PosMdjbtCmd(Bitmap mBitmap, Bitmap pic_bitmap, int nWidth, boolean bPic){
        int dstw = (nWidth + 7) / 8 * 8;
        int dsth = mBitmap.getHeight() * dstw / mBitmap.getWidth();

        byte []dotsmap = bitmap2PrintDotsmap(mBitmap, nWidth, bPic);

        int size = dotPixel2PosMdjbtCmd(dotsmap, null, dstw, dsth);
        if (size > 0){
            byte []buffer = new byte[size];
            size = dotPixel2PosMdjbtCmd(dotsmap, buffer, dstw, dsth);
            return buffer;
        }
        return null;
    }

    // 高级指令
    public static byte[] Bitmap2PosFastPictureCmd(Bitmap mBitmap, int nWidth, boolean bPic){
        int dstw = (nWidth + 7) / 8 * 8;
        int dsth = mBitmap.getHeight() * dstw / mBitmap.getWidth();

        byte []dotsmap = bitmap2PrintDotsmap(mBitmap, nWidth, bPic);

        int size = getPrintPictureCommand(null, dotsmap, dstw/8);
        if (size > 0){
            byte []buffer = new byte[size];
            size = getPrintPictureCommand(buffer, dotsmap, dstw/8);
            return buffer;
        }
        return null;
    }

    // TSPL
    public static byte[] bitmap2TsplDotsmap(Bitmap mBitmap, int nWidth, boolean bPicture, boolean bRev){
        int dstw = (nWidth + 7) / 8 * 8;
        int dsth = mBitmap.getHeight() * dstw / mBitmap.getWidth();
        boolean[] dithered = Pos.getBitmapDots(mBitmap, nWidth, bPicture);
        int w_bytes = (dstw + 7) /8;
        byte []dotsmap = new byte[w_bytes * dsth];

        convBitmap2Command(dithered, dotsmap, dstw, bRev);
        return dotsmap;
    }

}
